import { miniProgramList } from "@renderer/ajax/api/common";

/** 小程序列表 */
const miniApplist = []

/**
 * 获取小程序信息。单列
 * @returns 
 */
const getSingleMiniApp = async () => {
    if (miniApplist.length) {
        return miniApplist
    }

    miniApplist.length = 0
    const data = await getMiniProgramList()
    miniApplist.push(...(data || []))
    return miniApplist
}


// 获取小程序列表
const getMiniProgramList = async () => {
    const res = await miniProgramList({
        Page: 1,
        PageSize: 99999,
        keyword: ""
    })
    return res.data
}

/**
 * 根据小程序id获取小程序的信息
 * @param appid 小程序id
 */
export const getMiniAppInfoByAppid = async (AppId: string) => {
    const appInfo = miniApplist.find(app => app.AppId === AppId)
    if (appInfo) {
        return appInfo
    }

    miniApplist.length = 0
    const newAppList = (await getSingleMiniApp()) || []
    console.log('newAppList: ', newAppList);
    const newAppInfo = newAppList.find(app => app.AppId === AppId)
    
    return newAppInfo
}
